package com.example.demo_one

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.demo_one.fragment.DetailFragment
import com.example.demo_one.fragment.ListFragment

class MainActivity : AppCompatActivity(), ListFragment.OnItemClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        // 加载Fragment
        // 如果 savedInstanceState == null，则说明当前Activity是第一次创建，需要加载Fragment
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container, ListFragment())
                .commit()
        }
    }

    override fun onItemClick(position: Int) {
        // Toast.makeText(this, "点击了第 $position 个项目", Toast.LENGTH_SHORT).show()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, DetailFragment()) // 替换Fragment
            .addToBackStack(null)
            .commit()
    }
}